package com.zfzs.post.modules.policy.service.impl;

import com.zfzs.post.modules.policy.domain.Policy;
import com.zfzs.post.modules.policy.domain.PolicyInfo;
import com.zfzs.post.modules.policy.domain.PolicyKind;
import com.zfzs.post.modules.policy.repository.PolicyInfoRepository;
import com.zfzs.post.modules.policy.service.PolicyInfoService;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 政策设置
 *
 * @author: ck
 * @create: 2019/8/20/15:16
 **/
@Service
public class PolicyInfoServiceImpl implements PolicyInfoService {

    @Autowired
    private PolicyInfoRepository policyInfoRepository;

    @Override
    public PolicyInfo findOne(PolicyInfo policyInfo) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("agentId", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("terminalTypeId", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<PolicyInfo> ex = Example.of(policyInfo, matcher);
        Optional<PolicyInfo> p = policyInfoRepository.findOne(ex);
        return p.isPresent() ? p.get() : null;
    }

    @Override
    public PolicyKind findPolicy(Long agentId, Long terminalTypeId, String id) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setAgentId(agentId);
        policyInfo.setTerminalTypeId(terminalTypeId);
        PolicyInfo p = findOne(policyInfo);
        if (p == null) {
            return null;
        }
        //所有父政策
        List<PolicyKind> list = p.getList();
        for (int i = 0; i < list.size(); i++) {
            String pidId = list.get(i).getId();
            if (StringUtils.equals(id, pidId)) {
                //返回所属的政策信息
                return list.get(i);
            }
        }
        return null;
    }

    @Override
    public PolicyInfo findById(String id) {
        Optional<PolicyInfo> p = policyInfoRepository.findById(id);
        return p.isPresent() ? p.get() : null;
    }

    @Override
    public PolicyInfo save(PolicyInfo policyInfo) {
        return policyInfoRepository.save(policyInfo);
    }

    @Override
    public List<PolicyInfo> queryAll() {
        return policyInfoRepository.findAll();
    }

    @Override
    public Boolean isPresence(Long agentId, Long terminalTypeId) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setTerminalTypeId(terminalTypeId);
        policyInfo.setAgentId(agentId);
        PolicyInfo p = findOne(policyInfo);
        return p != null;
    }

    @Override
    public PolicyInfo findPolicyInfo(Long agentId, Long terminalTypeId) {
        PolicyInfo policyInfo = new PolicyInfo();
        policyInfo.setTerminalTypeId(terminalTypeId);
        policyInfo.setAgentId(agentId);
        PolicyInfo p = findOne(policyInfo);
        return p;
    }

    @Override
    public String findPolicyInfoAmount(Long agentId, Long terminalTypeId, String pidId, String policyTypeId) {
        PolicyKind policy = findPolicy(agentId, terminalTypeId, pidId);
        if (policy != null) {
            List<Policy> list = policy.getPolicyList();
            for (int i = 0; i < list.size(); i++) {
                String typeId = list.get(i).getPolicyTypeId();
                if (typeId.equals(policyTypeId)) {
                    return list.get(i).getAmount();
                }
            }
        } else {
            return null;
        }
        return null;
    }
}
